<html>
<head><meta charset="utf-8"><title>Windows: main thread stack size · t-compiler · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/index.html">t-compiler</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Windows.3A.20main.20thread.20stack.20size.html">Windows: main thread stack size</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="238514333"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Windows%3A%20main%20thread%20stack%20size/near/238514333" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Chris Denton <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Windows.3A.20main.20thread.20stack.20size.html#238514333">(May 12 2021 at 17:39)</a>:</h4>
<p>On Linux, the maximum stack size of the main thread is typically set by the OS. On Windows, it's requested by the application via a value in the executable's header.</p>
<p>Currently Rust does not specify a value for the main thread so, on Windows, the linker falls back to requesting only 1 MiB. I think it would be reasonable to raise this value to 4 or 8 MiB by default to be more in line with the default size of other threads. But if a default is set then it should overrideable somehow (a <code>-C</code> option maybe?).</p>
<p>Does this sound sensible so far? If so I might open an issue for it.</p>



<a name="238514856"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Windows%3A%20main%20thread%20stack%20size/near/238514856" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Windows.3A.20main.20thread.20stack.20size.html#238514856">(May 12 2021 at 17:42)</a>:</h4>
<p>If the linker sets it, <code>-Clink-arg</code> should already be able to configure it</p>



<a name="238515192"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Windows%3A%20main%20thread%20stack%20size/near/238515192" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Chris Denton <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Windows.3A.20main.20thread.20stack.20size.html#238515192">(May 12 2021 at 17:44)</a>:</h4>
<p>Sure. But I mean if Rust set a default then it should also have a way to override that default. Otherwise it'll be setting the argument twice, which could have unpredictable results.</p>



<a name="238516495"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Windows%3A%20main%20thread%20stack%20size/near/238516495" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Windows.3A.20main.20thread.20stack.20size.html#238516495">(May 12 2021 at 17:52)</a>:</h4>
<p>ah, true</p>



<a name="238795729"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Windows%3A%20main%20thread%20stack%20size/near/238795729" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> tmandry <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Windows.3A.20main.20thread.20stack.20size.html#238795729">(May 14 2021 at 16:49)</a>:</h4>
<p>on fuchsia we had a lot of problems with the stack size of the main thread differing from the default stack size of spawned threads, so we made the main thread match the spawned thread size (2MiB by default)</p>



<a name="238795786"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Windows%3A%20main%20thread%20stack%20size/near/238795786" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> tmandry <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Windows.3A.20main.20thread.20stack.20size.html#238795786">(May 14 2021 at 16:49)</a>:</h4>
<p>the problem was you’d move code from a worker thread to the main thread and suddenly you’d get stack overflows</p>



<a name="238795887"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Windows%3A%20main%20thread%20stack%20size/near/238795887" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> tmandry <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Windows.3A.20main.20thread.20stack.20size.html#238795887">(May 14 2021 at 16:50)</a>:</h4>
<p>this happened a lot with async</p>



<a name="238795967"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Windows%3A%20main%20thread%20stack%20size/near/238795967" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> tmandry <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Windows.3A.20main.20thread.20stack.20size.html#238795967">(May 14 2021 at 16:50)</a>:</h4>
<p>there’s a linker flag to set it. let me dig it up</p>



<a name="238796294"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Windows%3A%20main%20thread%20stack%20size/near/238796294" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> tmandry <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Windows.3A.20main.20thread.20stack.20size.html#238796294">(May 14 2021 at 16:53)</a>:</h4>
<p><code>-Clink-args=-zstack-size=0x200000</code></p>



<a name="238796665"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Windows%3A%20main%20thread%20stack%20size/near/238796665" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Windows.3A.20main.20thread.20stack.20size.html#238796665">(May 14 2021 at 16:56)</a>:</h4>
<p>it does seem like main's default should <em>at least</em> be as much as we give threads</p>



<a name="238796757"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Windows%3A%20main%20thread%20stack%20size/near/238796757" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Windows.3A.20main.20thread.20stack.20size.html#238796757">(May 14 2021 at 16:57)</a>:</h4>
<p>Linux gives you more -- dynamically growing, unless rlimit says otherwise</p>



<a name="238803914"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Windows%3A%20main%20thread%20stack%20size/near/238803914" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Windows.3A.20main.20thread.20stack.20size.html#238803914">(May 14 2021 at 17:46)</a>:</h4>
<p>oh, I didn't know spawned threads get more, that seems odd?</p>



<a name="238822865"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Windows%3A%20main%20thread%20stack%20size/near/238822865" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Chris Denton <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Windows.3A.20main.20thread.20stack.20size.html#238822865">(May 14 2021 at 20:10)</a>:</h4>
<p><span class="user-mention silent" data-user-id="138448">cuviper</span> <a href="#narrow/stream/131828-t-compiler/topic/Windows.3A.20main.20thread.20stack.20size/near/238796757">said</a>:</p>
<blockquote>
<p>Linux gives you more -- dynamically growing, unless rlimit says otherwise</p>
</blockquote>
<p>Windows has two values an application can set: the amount of virtual memory to reserve for the stack and the amount of physical memory to commit immediately. Both will be rounded up to the nearest page size. So the stack can grow dynamically from the committed amount to the reserved amount.</p>



<a name="238823526"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Windows%3A%20main%20thread%20stack%20size/near/238823526" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Chris Denton <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Windows.3A.20main.20thread.20stack.20size.html#238823526">(May 14 2021 at 20:16)</a>:</h4>
<p>Tbh I'm not sure there's really any downside to setting something large like 16 MiB or more for a 64-bit target. Though on 32-bit virtual memory is more limited. In any case, I do think it should at least match the default for new threads. I'll open an issue.</p>



<a name="238826992"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Windows%3A%20main%20thread%20stack%20size/near/238826992" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Windows.3A.20main.20thread.20stack.20size.html#238826992">(May 14 2021 at 20:43)</a>:</h4>
<p>On Windows both commited and reserved amount counts towards your "total" RAM utilization, I believe, so with a larger reserved stack you'd be able to run fewer binaries, either way?</p>



<a name="238827070"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Windows%3A%20main%20thread%20stack%20size/near/238827070" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Windows.3A.20main.20thread.20stack.20size.html#238827070">(May 14 2021 at 20:44)</a>:</h4>
<p>I think the difference is that windows wouldn't page out the other allocated pages into the pagefile in low memory situations.</p>



<a name="238830422"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Windows%3A%20main%20thread%20stack%20size/near/238830422" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Chris Denton <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Windows.3A.20main.20thread.20stack.20size.html#238830422">(May 14 2021 at 21:11)</a>:</h4>
<p>I'm not sure I understand you? If it's only reserved then it doesn't count for anything important. The memory doesn't actually exist yet. It's just taking a bite out of your application's own personal virtual memory.  If it does get committed then standard rules apply. Some pages of memory may be in actual memory or in the page file.</p>



<a name="238830958"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Windows%3A%20main%20thread%20stack%20size/near/238830958" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Windows.3A.20main.20thread.20stack.20size.html#238830958">(May 14 2021 at 21:14)</a>:</h4>
<p>Hm, okay. Though I'm confused as to who's responsible for actually committing it in that case.</p>



<a name="238831062"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Windows%3A%20main%20thread%20stack%20size/near/238831062" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Windows.3A.20main.20thread.20stack.20size.html#238831062">(May 14 2021 at 21:15)</a>:</h4>
<p>Windows is generally fairly inside the camp of not having hidden allocations that can fail without you invoking _some_ API to request memory.</p>



<a name="238833277"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Windows%3A%20main%20thread%20stack%20size/near/238833277" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Chris Denton <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Windows.3A.20main.20thread.20stack.20size.html#238833277">(May 14 2021 at 21:37)</a>:</h4>
<p>Hm, well I just experimented with spawning multiple processes with insanely large stack size (and a pause to keep them open). There does seem to be a maximum amount you can reserve per application, which on my system is roughly <code>0x7DCED9C9FFF7</code>. I was able to build and run multiple of these applications at once.</p>



<a name="238833380"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Windows%3A%20main%20thread%20stack%20size/near/238833380" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Chris Denton <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Windows.3A.20main.20thread.20stack.20size.html#238833380">(May 14 2021 at 21:38)</a>:</h4>
<p>Maybe I should try running them in a loop and see when I hit a limit.</p>



<a name="238915323"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Windows%3A%20main%20thread%20stack%20size/near/238915323" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Chris Denton <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Windows.3A.20main.20thread.20stack.20size.html#238915323">(May 15 2021 at 18:06)</a>:</h4>
<p>I happened to find this while looking for something else memory related: <a href="http://proof%20that%20Windows%20is%20overcommitting%20stacks">https://superuser.com/questions/1194263/will-microsoft-windows-10-overcommit-memory/1546584#1546584</a>. Admittedly I haven't check their working.</p>



<a name="238923636"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Windows%3A%20main%20thread%20stack%20size/near/238923636" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> rpjohnst <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Windows.3A.20main.20thread.20stack.20size.html#238923636">(May 15 2021 at 20:34)</a>:</h4>
<p>that's my understanding as well- you can reserve more than your physical memory but you can't reserve more than your physical memory+swap space. uncommitted reserved memory doesn't need to be backed by any <em>specific</em> page of ram or disk but once you touch it the kernel will give you ram, possibly by paging something else out (which is guaranteed to succeed because of the reserve limit).</p>



<a name="238925318"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Windows%3A%20main%20thread%20stack%20size/near/238925318" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Windows.3A.20main.20thread.20stack.20size.html#238925318">(May 15 2021 at 21:04)</a>:</h4>
<p>On windows "reserve" only reserves address space, not physical memory. It's commit that actually allocates physical memory and that's when you can hit the commit limit.</p>



<a name="239124407"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Windows%3A%20main%20thread%20stack%20size/near/239124407" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> rpjohnst <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Windows.3A.20main.20thread.20stack.20size.html#239124407">(May 17 2021 at 16:57)</a>:</h4>
<p><span class="user-mention silent" data-user-id="143274">Amanieu</span> <a href="#narrow/stream/131828-t-compiler/topic/Windows.3A.20main.20thread.20stack.20size/near/238925318">said</a>:</p>
<blockquote>
<p>On windows "reserve" only reserves address space, not physical memory. It's commit that actually allocates physical memory and that's when you can hit the commit limit.</p>
</blockquote>
<p>In general yes, but i don't think it can possibly work that way for the stack, can it?</p>



<a name="239124478"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Windows%3A%20main%20thread%20stack%20size/near/239124478" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> rpjohnst <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Windows.3A.20main.20thread.20stack.20size.html#239124478">(May 17 2021 at 16:58)</a>:</h4>
<p>Because there's no commit call to signal errors when you grow into the reserved portion of the stack</p>



<a name="239127220"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Windows%3A%20main%20thread%20stack%20size/near/239127220" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> rpjohnst <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Windows.3A.20main.20thread.20stack.20size.html#239127220">(May 17 2021 at 17:16)</a>:</h4>
<p>Or hmm, maybe i'm missing something here- it looks like accessing a guard page raises an SEH exception, which is normally what commits more stack, though i can't find very detailed documentation... so perhaps an OOM there would just be like an ordinary stack overflow?</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>